package com.jci.vsd.activity.enterprise;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jci.vsd.R;
import com.jci.vsd.activity.BaseActivity;
import com.jci.vsd.activity.LoginActivity;

import com.jci.vsd.adapter.enterprise.MembersManagerAdapter;
import com.jci.vsd.bean.enterprise.AjustMembersBean;
import com.jci.vsd.bean.enterprise.MembersBean;
import com.jci.vsd.network.control.MembersManageControl;
import com.jci.vsd.observer.CommonDialogObserver;
import com.jci.vsd.observer.RxHelper;
import com.jci.vsd.utils.Loger;
import com.jci.vsd.view.widget.SimpleToast;
import com.jci.vsd.view.widget.treelist.TreeMenuBaseAdapter;
import com.jci.vsd.view.widget.treelist.bean.Node;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Created by liqing on 18/6/29.
 * （老板类型）成员管理
 */

public class MembsersManageActivity extends BaseActivity {
    private static final int REQUEST_CODE_DEPARTMENT = 1111;
    @BindView(R.id.lv_members)

    ListView lvMembers;
    private MembersManagerAdapter<MembersBean> adapter; //人员列表adapter
    private List<MembersBean> mData;//人员集合

    private Dialog dialog;
    private View inflate;
    private TextView cancel;

    private Context context;
    private TextView tvAjust;
    private TextView tvDelete;
    private int deletePos;//删除位置
    private MembersBean selectOperateBean;//选中位置
    private List<MembersBean> departmentList;//部门成员集合

    @BindView(R.id.button_back)
    ImageButton backBtn;
    @BindView(R.id.textview_title)
    TextView titleTxt;
    private String deleteUid;//删除人员id
    private String selectUid;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_members_manage);
        context = MembsersManageActivity.this;
        mData = new ArrayList<>();
        departmentList = new ArrayList<>();
        initTestData();
        initViewEvent();
        loadData();


    }


    public void showDialog() {
        dialog = new Dialog(this, R.style.BottomDialog);
        inflate = LayoutInflater.from(this).inflate(R.layout.dialog_choose_role, null);
        tvDelete = (TextView) inflate.findViewById(R.id.tv_register_company);
        tvAjust = (TextView) inflate.findViewById(R.id.tv_select_company);
        cancel = (TextView) inflate.findViewById(R.id.btn_cancel);
        tvAjust.setText("调入其他部门");
        tvDelete.setText("从本公司删除");
        tvAjust.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
        cancel.setOnClickListener(this);
        dialog.setContentView(inflate);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        // WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //获得window窗口的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        //将自定义布局加载到dialog上
        dialog.setContentView(inflate);
        dialog.show();
    }


    // idD是唯一标志， 部门id  人员ID  另外字段表示
    private void initTreeMemberAdapter() {
        try {
            adapter = new MembersManagerAdapter<>(lvMembers, this, mData, 0);
            //  adapter.setListExpand(false);
            adapter.setTreeNodeClickListener(new TreeMenuBaseAdapter.OnTreeNodeClickListener() {
                @Override
                public void onNodeClick(Node node, int position) {
                    Loger.e("---node--");
                    adapter.setCurrentPosition(position, node.isExpand());
                    adapter.notifyDataSetChanged();
                }

            });

            //长按
            adapter.setTreeNodeLongClickListener(new TreeMenuBaseAdapter.OnTreeNodeLongClickListener() {
                @Override
                public void onNodeLongClick(Node node, int position) {
                    //是个子节点 (是个部门,不可以长按)原来：node.getChildrenNodes().size() == 0
                    if (!node.getParentID().equals("0")) {
                        selectOperateBean = new MembersBean();
                        //userIf
                        selectUid = node.getUid();
                        //所属部门ID
                        String selectPid = node.getParentID();

                        Loger.e("nodeLongClick--pos=" + position + "nodeUid=" + node.getUid() + "nodePid=" + selectPid);
                        Loger.e("nodeName-" + node.getNodeName());
                        Loger.e("nodeLeader-" + node.getLeader());

                        deleteUid = node.getUid();
                        selectOperateBean.setUid(selectUid);
                        selectOperateBean.setDpt(selectPid);
                        selectOperateBean.setId(node.getNodeID());
                        selectOperateBean.setName(node.getNodeName());

//                        selectOperateBean = mData.get(position);
                        Loger.e("--selectOperateBean.name=" + selectOperateBean.getName() +
                                "selectOperateBean.uid=" + selectOperateBean.getUid());
                        showDialog();
                        deletePos = position;

                        //调入部门去掉当前部门
//                        for (int i = 0; i < departmentList.size(); i++) {
//                            if (departmentList.get(i).getId().equals(selectPid)) {
//                                departmentList.remove(i);
//                            }
//                        }
//
//                        items = new String[departmentList.size()];
//                        for (int i = 0; i < departmentList.size(); i++) {
//                            items[i] = departmentList.get(i).getName();
//                            Loger.e(" 部门.--" + items[i]);
//                        }
//                        for (int i = 0; i < mData.size(); i++) {
//                            Loger.e(" mData.--" + mData.get(i).getName());
//                        }

                    }

                    adapter.setCurrentPosition(position, node.isExpand());
                    adapter.notifyDataSetChanged();
                }
            });
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        lvMembers.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_DEPARTMENT) {

            }

        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_select_company:
           //ajust 调整部门 用dialog
                showSingleChoiceDialog();
                dialog.dismiss();
                break;
            case R.id.tv_register_company:
                //从本公司删除成员
                Loger.e("-----delete Uid" + deleteUid);
                selectOperateBean.setUid(deleteUid);
                deleteMember(selectOperateBean);
                dialog.dismiss();
                break;
            case R.id.btn_cancel:
                dialog.dismiss();
                break;
            default:
                break;
        }
    }

    String[] items = new String[]{};

    //掉入其他部门
    private void showSingleChoiceDialog() {
Loger.e("---掉入其他部门--");
        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("单选对话框").setIcon(R.drawable.ic_image)
                .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Loger.e("choose-whitch--" + which);
                        AjustMembersBean ajustMembersBean = new AjustMembersBean();
                        ajustMembersBean.setDpt(Integer.valueOf(departmentList.get(which).getDpt()));
                        ajustMembersBean.setId(Integer.valueOf(selectUid));
                        // ajustMembersBean.setId(Integer.valueOf(selectOperateBean.getId()));
                        Loger.e("dptId=" + departmentList.get(which).getDpt());
                        Loger.e("ajustPersonid=" + selectUid);
                        ajust(ajustMembersBean);
                        dialog.dismiss();

                    }
                }).create();
        dialog.show();

    }


//    //审批人选择单选框singleChoicea
//    private void showSingleChoiceDialog() {
//
//        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("单选对话框").setIcon(R.drawable.ic_image)
//                .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
//
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        Loger.e("choose-whitch--" + which);
//
//                        dialog.dismiss();
//
//                    }
//                }).create();
//        dialog.show();
//
//    }

    @Override
    protected void initViewEvent() {
        backBtn.setOnClickListener(this);
        titleTxt.setText(getResources().getString(R.string.members_manage));
    }


    //测试用
    private void initTestData() {
        //parentItem
//        mData.add(new MembersBean("1", "0", "总经理办公室"));
//        mData.add(new MembersBean("2", "0", "销售部"));
//        //总经理下的childItem
//        mData.add(new MembersBean("3", "1", "张三"));
//        mData.add(new MembersBean("4", "1", "王一"));
//
//        //销售部下的
//
//        mData.add(new MembersBean("5", "2", "孙田"));
//        mData.add(new MembersBean("6", "2", "Jsec"));
//        mData.add(new MembersBean("7", "2", "Lotey"));
        MembersBean dpt1 = new MembersBean();
        dpt1.setPid("0");
        dpt1.setId("1");
        dpt1.setName("总经理办公室");
        // dpt1.setLeader(false);
        mData.add(dpt1);

        MembersBean dpt2 = new MembersBean();
        dpt2.setPid("1");
        dpt2.setId("11");
        dpt2.setName("jess");
        dpt2.setLeader(true);
        mData.add(dpt2);

        MembersBean dpt3 = new MembersBean();
        dpt3.setPid("1");
        dpt3.setId("12");
        dpt3.setName("cherry");
        dpt3.setLeader(false);
        mData.add(dpt3);

        items = new String[4];
        items[0] = "23";
        items[1] = "234";
        items[2] = "235";
        items[3] = "236";
        initTreeMemberAdapter();

    }


    //加载MemberBean
    private void loadData() {
        Observable<List<MembersBean>> observable = new MembersManageControl().getMembers();
        CommonDialogObserver<List<MembersBean>> observer = new CommonDialogObserver<List<MembersBean>>(this) {
            @Override
            public void onNext(List<MembersBean> beanList) {
                super.onNext(beanList);
                mData.clear();
                departmentList.clear();
                for (int i = 0; i < beanList.size(); i++) {
                    if (beanList.get(i).getPid().equals("0")) {
                        //加入部门集合
                        departmentList.add(beanList.get(i));
                        items = new String[departmentList.size()];
                        for (int j = 0; j < departmentList.size(); j++) {
                            items[j] = departmentList.get(j).getName();
                            Loger.e(" 部门.--" + items[j]);
                        }
                        //部门的node节点肯定不是leader,且设置uid=-1
                        beanList.get(i).setLeader(false);
                        if (beanList.get(i).getUid() == null) {
                            beanList.get(i).setUid("-1");
                        }

                    }
                }
                mData.addAll(beanList);

                initTreeMemberAdapter();


            }


            @Override
            public void onError(Throwable t) {
                super.onError(t);
            }
        };
        RxHelper.bindOnUIActivityLifeCycle(observable, observer, MembsersManageActivity.this);
    }

    //delete members

    private void deleteMember(MembersBean membersBean) {
        Loger.e("delete-uid---" + membersBean.getUid());
        Observable<Boolean> observable = new MembersManageControl().deleteMember(membersBean);
        CommonDialogObserver<Boolean> observer = new CommonDialogObserver<Boolean>(this) {
            @Override
            public void onNext(Boolean aBoolean) {
                super.onNext(aBoolean);
                if (aBoolean) {
                    SimpleToast.toastMessage("删除成功", Toast.LENGTH_LONG);
                    loadData();
                } else {
                    SimpleToast.toastMessage("删除失败，请重试！", Toast.LENGTH_LONG);
                }
            }

            @Override
            public void onError(Throwable t) {
                super.onError(t);
            }
        };
        RxHelper.bindOnUIActivityLifeCycle(observable, observer, MembsersManageActivity.this);
    }

    //调整到新部门 （ 新部门新增成员，就部门减一人）

    private void ajust(AjustMembersBean requestBean) {
        Observable<Boolean> observable = new MembersManageControl().ajustMember(requestBean);
        CommonDialogObserver<Boolean> observer = new CommonDialogObserver<Boolean>(this) {
            @Override
            public void onNext(Boolean aBoolean) {
                super.onNext(aBoolean);
                if (aBoolean) {
                    SimpleToast.toastMessage("操作成功", Toast.LENGTH_LONG);
                    loadData();
                } else {
                    SimpleToast.toastMessage("操作失败，请重试！", Toast.LENGTH_LONG);
                }
            }

            @Override
            public void onError(Throwable t) {
                super.onError(t);

                if (t.getMessage().equals("401")) {
                    SimpleToast.toastMessage("登录超时，请重新登录", Toast.LENGTH_SHORT);
                    exit();
                    toActivity(LoginActivity.class);
                }
            }
        };
        RxHelper.bindOnUIActivityLifeCycle(observable, observer, MembsersManageActivity.this);
    }


}
